package com.example.presentshopping.ui.activity

import BaseRecyclerAdapter
import BaseRecyclerHolder
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpActivity
import com.example.presentshopping.config.bean.SupplierListBean
import com.example.presentshopping.config.bean.TurntableResBean
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.utils.tool.GsonUtils
import kotlinx.android.synthetic.main.activity_turntable_res.*

class TurntableResActivity : BaseMvpActivity() {

    var  Turntable : TurntableResBean?=null
    var page = 1
    private var queryInfoPresenter: PublicPresenter? = null
    var baseRecyclerAdapter: BaseRecyclerAdapter<TurntableResBean.DataBean>? = null

    override val layoutId: Int
        get() = R.layout.activity_turntable_res

    override fun initView(savedInstanceState: Bundle?) {
        queryInfoPresenter = PublicPresenter(this, false, "")
        queryInfoPresenter?.attachView(this)
        title_main_tv.leftBackImageTv.setOnClickListener { finish() }
        title_main_tv.titleTv.text ="中奖纪录"
        setAdapter()


    }

    override fun onResume() {
        super.onResume()
        baseRecyclerAdapter?.clear()
        geurntableRes()

    }
    fun setAdapter(){
        baseRecyclerAdapter = object :BaseRecyclerAdapter<TurntableResBean.DataBean>(this,null,R.layout.iemt_turntableres){
            override fun convert(
                holder: BaseRecyclerHolder?,
                data: TurntableResBean.DataBean,
                position: Int,
                isScrolling: Boolean,
                selectedPosition: Int
            ) {
                 val name_tv =holder?.getView<TextView>(R.id.name_tv)
                val create_time = holder?.getView<TextView>(R.id.create_time)
                val tv_status = holder?.getView<TextView>(R.id.tv_status)
                name_tv?.text="奖品: "+data.name
                create_time?.text =data.create_time
                val status =data?.status



                tv_status?.setOnClickListener {
                    val init = Intent(context,TurntableAddressActivity().javaClass)
                    init.putExtra("id",data.id.toString())
                    init.putExtra("name",data.name)
                    startActivity(init)

                }

                if(data.tel != null){
                    tv_status?.text ="已领取"
                    tv_status?.isClickable=false
                }else{
                    tv_status?.text ="领取奖励"
                    tv_status?.isClickable=true
                }



            }

        }
        recycler_view?.adapter = baseRecyclerAdapter
    }


    fun geurntableRes() {
        queryInfoPresenter?.netWorkRequestGet(HttpURL.Req_getTurntableRes)
    }

    override fun onDestroy() {
        queryInfoPresenter?.detachView()
        super.onDestroy()
    }

    override fun showToast(msg: String?) {


    }

    override fun onSuccess(data: String?) {
        Turntable  =GsonUtils.getGson().fromJson(data,TurntableResBean().javaClass)
        baseRecyclerAdapter?.setData(Turntable?.data)
    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

    }


}